package com.turning.resume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.turning.resume.common.RegexConstant;
import com.turning.resume.common.Result;
import com.turning.resume.dto.ResumeDTO;
import com.turning.resume.mapper.ResumeMapper;
import com.turning.resume.service.ResumeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, ResumeDTO> implements ResumeService {

    private final ResumeMapper resumeMapper;

    @Override
    public Result insert(ResumeDTO resumeDTO) {
        //1.如果为空直接返回
        if (resumeDTO == null) {
            return Result.error("不写简历还想进图灵，(╬▔皿▔)╯");
        }

        //2.判断是否已经填写过简历
        QueryWrapper<ResumeDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", resumeDTO.getName());
        queryWrapper.eq("major", resumeDTO.getMajor());
        queryWrapper.eq("class", resumeDTO.getClazz());
        queryWrapper.eq("phone", resumeDTO.getPhone());
        queryWrapper.eq("email", resumeDTO.getEmail());
        ResumeDTO resume = getOne(queryWrapper);

        //2.1填写过修改，然后返回true,前端打印简历
        if (resume != null) {
            //修改
            resumeMapper.update(resumeDTO, queryWrapper);
            //返回
            return Result.success();
        }

        //3.检查格式
        //3.1验手机号的格式
        String phone = resumeDTO.getPhone();
        System.out.println(phone);
        if (phone.matches(RegexConstant.PHONE_REGEX)) {
            return Result.error("请输入正确的手机号码，不然顺着网线过来真实你，(❁´◡`❁)");
        }

        //3.2检查邮箱格式
        String email = resumeDTO.getEmail();
        if (email.matches(RegexConstant.EMAIL_REGEX)) {
            return Result.error("请输入正确的邮箱地址，不然顺着网线过来真实你，(❁´◡`❁)");
        }

        //4.保存
        save(resumeDTO);

        //5.返回
        return Result.success();
    }
}
